package com.jxwifi.cloud.quickcleanserver.utils;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jxwifi.cloud.quickcleanserver.app.AccountMgr;
import com.jxwifi.cloud.quickcleanserver.app.UrlPool;
import com.jxwifi.cloud.quickcleanserver.login.LoginActivity;
import com.jxwifi.cloud.quickcleanserver.okhttp.AuzHttp;
import com.jxwifi.cloud.quickcleanserver.okhttp.DataJson_Cb;
import com.jxwifi.cloud.quickcleanserver.okhttp.Params;

/**
 * Created by Administrator on 2019/5/20.
 */

public class PasswordLogin {

    public static boolean mIsLogin=true;

    public static void passwordlogin(final Context context, String TAG, String tell, String password){

        Params params=new Params();
        params.add("password",password);
        params.add("tell",tell);
        AuzHttp.get(UrlPool.GET_PASSWORD_LOGIN, params, new DataJson_Cb() {
            @Override
            public void onSuccess(int code, String data) {
                Log.w("oushuhua","接口GET_PASSWORD_LOGIN请求errorCode=="+code);
                JSONObject obj = JSON.parseObject(data);
                String token=obj.getString("token");
                AccountMgr.puttoken(context,token);
                //AuzHttp.KJB_APP_Token=AccountMgr.gettoken(context);   //token添加到请求头
                AuzHttp.KJB_APP_Token=AccountMgr.gettoken(context);
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                Log.w("oushuhua","接口GET_PASSWORD_LOGIN请求errorCode=="+errorCode);
                if (errorCode==402){
                    mIsLogin=false;
                    Toast toast =Toast.makeText(context, "您在其他手机登录了，请重新登录", Toast.LENGTH_SHORT);
                    ToastUtils.showMyToast(toast,3000);
                    AccountMgr.logout(context);
                    Intent intent=new Intent();
                    intent.setClass(context, LoginActivity.class);
                    context.startActivity(intent);
                }else if(errorCode==405){//在并发请求的时候会多次出发405报错信息，不可显示给客户，做处理
                    Log.w("oushuhua","errorCode 405 并发请求时特殊处理");
                }else{
                    Toast toast =Toast.makeText(context, msg, Toast.LENGTH_SHORT);
                    ToastUtils.showMyToast(toast,3000);
                }
            }
        },TAG);
    }
}
